<?php
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once('./includes/Response.php');
require_once('./includes/picture.php');

if($_REQUEST['act'] == 'personal_card'){
    $user_id = isset($_REQUEST['user_id'])?trim($_REQUEST['user_id']):'';
    $visited_id = isset($_REQUEST['visited_id'])?trim($_REQUEST['visited_id']):'';

    if(empty($user_id)){
        Response::json(472,"用户信息获取失败");
    }
    if(empty($visited_id)){
        Response::json(473,"被访问者信息获取失败");
    }
    if($user_id == $visited_id){
        $relationship = 0;//用户自己
    }elseif(is_friend($user_id,$visited_id)){
        $relationship = 1;//好友
    }elseif(is_blacklist($user_id,$visited_id)){
        $relationship = 2;//黑名单
    }else{
        $relationship = 3;//陌生人
    }
    $sql = "SELECT user_id,user_name,user_pic,user_sex,user_sign,constellation,address,phone FROM ".$ecs->table('user')." WHERE user_id = '$visited_id'";
    $row = $db->getAll($sql);
    if(!empty($row)) {
        $arr = array(
            'relationship' => $relationship,
            'personal_card' => $row
        );
        Response::json(200, '数据返回成功',$arr);
    }else{
        Response::json(474,"数据获取失败");
    }

}
if($_REQUEST['act'] == 'change_card' && $_POST['relationship'] == 0){
    $user_id = isset($_REQUEST['user_id'])?trim($_REQUEST['user_id']):'';
    $address = isset($_REQUEST['address'])?trim($_REQUEST['address']):'';
    $user_sign = isset($_REQUEST['user_sign'])?trim($_REQUEST['user_sign']):'';

    if(empty($address)){
        Response::json(475,"地址不能为空");
    }if(mb_strlen($user_sign)>30){
        Response::json(476,"个人签名最多为30个字",$user_sign);
    }
    if(empty($_FILES['user_pic']['name'])){
        Response::json(477,"头像不能为空");
    }

    $user_pic = picture::uploaddir($_FILES['user_pic']['name'],$_FILES['user_pic']['tmp_name']);


    $sql = "UPDATE ".$ecs->table('user')." SET address='$address',user_sign='$user_sign',user_pic='$user_pic[0]' WHERE picture_id = '$picture_id";
    $db->query($sql);
    $row = mysql_affected_rows();
    if($row > 0){
        Response::json(200,"修改成功");
    }else{
        Response::json(478,"修改失败");
    }
} elseif($_POST['act'] == 'personal_info' && ($_POST['relationship'] == (0 || 1))){
    $user_id = isset($_REQUEST['user_id'])?trim($_REQUEST['user_id']):'';
    $sql = "SELECT * FROM ".$ecs->table('user')." WHERE user_id = '$user_id'";
    $row = $db->getAll($sql);
    if(!empty($row)) {
        $sql = "SELECT activity_id FROM ".$ecs->table('user_activity')." WHERE user_id = '$user_id'";
        $row1 = $db->getAll($sql);
        $count = count($row1);
        $arr = array(
            'activity_num' => $count,
            'personal_card' => $row
        );
        Response::json(200, '数据返回成功',$arr);
    }else{
        Response::json(474,"数据获取失败");
    }

}elseif($_REQUEST['act'] == 'change_personal_info' && $_POST['relationship'] == 0){
    $user_id = isset($_REQUEST['user_id'])?trim($_REQUEST['user_id']):'';
    $address = isset($_REQUEST['address'])?trim($_REQUEST['address']):'';
    $user_sign = isset($_REQUEST['user_sign'])?trim($_REQUEST['user_sign']):'';
    $emotion = isset($_REQUEST['emotion'])?trim($_REQUEST['emotion']):'';
    $profession = isset($_REQUEST['profession'])?trim($_REQUEST['profession']):'';
    $birth = isset($_REQUEST['birth'])?trim($_REQUEST['birth']):'';
    $constellation = isset($_REQUEST['constellation'])?trim($_REQUEST['constellation']):'';
    $hometown = isset($_REQUEST['hometown'])?trim($_REQUEST['hometown']):'';

    if (empty($birth)) {
        return Response::json(402, "生日不能为空", $birth);
    }
    if (empty($address)) {
        return Response::json(403, "常住地不能为空", $address);
    }
    if(mb_strlen($user_sign)>30){
        Response::json(476,"个人签名最多为30个字",$user_sign);
    }
    $sql = "UPDATE ".$ecs->table('user')." SET address='$address',user_sign='$user_sign',emotion='$emotion',profession='$profession',birth='$birth',constellation='$constellation',hometown='$hometown' WHERE picture_id = '$picture_id";
    $db->query($sql);
    $row = mysql_affected_rows();
    if($row > 0){
        Response::json(200,"修改成功");
    }else{
        Response::json(478,"修改失败");
    }




}
//通讯录
elseif($_REQUEST['act'] == 'address_list'){
    $user_id = isset($_REQUEST['user_id'])?trim($_REQUEST['user_id']):'';
    if(empty($user_id)){
        Response::json(479,"豆瓣号获取失败");
    }else{
        $sql = "SELECT ecs_user.user_name,ecs_user.user_pic,ecs_user.user_sex FROM ".$ecs->table('friend')."LEFT JOIN ".$ecs->table('user')."ON ecs_user.user_id=ecs_friend.friend_id"." WHERE (ecs_friend.user_id ='$user_id' and ecs_friend.status=1)"."ORDER BY
	CONVERT (ecs_user.user_name USING gbk) COLLATE gbk_chinese_ci ASC";
        $row = $db->getAll($sql);
        $arr = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
        foreach ($arr as $key=>$value){
            foreach ($row as $val){
                if(getFirstCharter($val['user_name']) == $value){
                    $result[$key]['name'] = $val['user_name'];
                    $result[$key]['pic'] = $val['pic'];
                    $result[$key]['sex'] = $val['sex'];

                }

            }

        }
        if(!empty($result)){
        Response::json(204,"获取好友信息成功",$result);}
        else{
            Response::json(433,"好友信息读取失败");
        }
    }
}
//是否在好友列表中
function is_friend($user_id,$visited_id){
    $sql = "SELECT  friend_id FROM ".$GLOBALS['ecs']->table('friend')." WHERE (user_id = '$user_id' and status=1)";
    $friend_id = $GLOBALS['db']->getAll($sql);
    foreach ($friend_id as $key=>$value){
        if($value['friend_id'] == $visited_id){
            return ture;
        }
    }
}
//是否在黑名单中
function is_blacklist($user_id,$visited_id){
    $sql = "SELECT  black_id FROM ".$GLOBALS['ecs']->table('blacklist')." WHERE (user_id = '$user_id' and status=1)";
    $black_id = $GLOBALS['db']->getAll($sql);
    foreach ($black_id as $key=>$value){
        if($value['black_id'] == $visited_id){
            return ture;
        }
    }
}
//php获取中文字符拼音首字母
function getFirstCharter($str){
    if(empty($str)){return '';}
    $fchar=ord($str{0});
    if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str{0});
    $s1=iconv('UTF-8','gb2312',$str);
    $s2=iconv('gb2312','UTF-8',$s1);
    $s=$s2==$str?$s1:$str;
    $asc=ord($s{0})*256+ord($s{1})-65536;
    if($asc>=-20319&&$asc<=-20284) return 'A';
    if($asc>=-20283&&$asc<=-19776) return 'B';
    if($asc>=-19775&&$asc<=-19219) return 'C';
    if($asc>=-19218&&$asc<=-18711) return 'D';
    if($asc>=-18710&&$asc<=-18527) return 'E';
    if($asc>=-18526&&$asc<=-18240) return 'F';
    if($asc>=-18239&&$asc<=-17923) return 'G';
    if($asc>=-17922&&$asc<=-17418) return 'H';
    if($asc>=-17417&&$asc<=-16475) return 'J';
    if($asc>=-16474&&$asc<=-16213) return 'K';
    if($asc>=-16212&&$asc<=-15641) return 'L';
    if($asc>=-15640&&$asc<=-15166) return 'M';
    if($asc>=-15165&&$asc<=-14923) return 'N';
    if($asc>=-14922&&$asc<=-14915) return 'O';
    if($asc>=-14914&&$asc<=-14631) return 'P';
    if($asc>=-14630&&$asc<=-14150) return 'Q';
    if($asc>=-14149&&$asc<=-14091) return 'R';
    if($asc>=-14090&&$asc<=-13319) return 'S';
    if($asc>=-13318&&$asc<=-12839) return 'T';
    if($asc>=-12838&&$asc<=-12557) return 'W';
    if($asc>=-12556&&$asc<=-11848) return 'X';
    if($asc>=-11847&&$asc<=-11056) return 'Y';
    if($asc>=-11055&&$asc<=-10247) return 'Z';
    return null;
}